package cn.student.controller;

import cn.student.config.web.DataGridResultInfo;
import cn.student.config.web.ResponseMessage;
import cn.student.config.web.ResponseStatusCode;
import cn.student.pojo.Student;
import cn.student.pojo.dto.StudentDTO;
import cn.student.pojo.vo.JwtUser;
import cn.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 毛苏宇
 * User: me
 * Date: 2020/6/24
 * Time: 17:17
 * Description:
 */
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 删除学生
     *
     * @param userId
     * @return
     */
    @PostMapping("/delete")
    public ResponseMessage delete(@RequestParam("userId") long userId) {
        final boolean remove = studentService.remove(userId);
        if (remove) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "删除成功", "");
        }
        return new ResponseMessage(ResponseStatusCode.CODE_NOT_EXISTS, "无该用户", "");
    }

    /**
     * get学生扩展信息
     *
     * @return
     */
    @PostMapping("/list")
    public ResponseMessage getStudentInfo(@RequestParam(value = "page", defaultValue = "1") int page,
                                          @RequestParam(value = "rows", defaultValue = "5") int rows) {
        DataGridResultInfo<StudentDTO> dataGridResultInfo = studentService.find(page, rows);
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "查询成功", dataGridResultInfo);
    }


    /**
     * 插入学生信息
     *
     * @param student
     * @return
     */
    @PostMapping("/save")
    public ResponseMessage save(Student student) {
        boolean addStudent = studentService.add(student);
        if (addStudent) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "添加成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "添加失败", null);
    }


    @PostMapping("/updateMyslef")
    public ResponseMessage updateMyslef(JwtUser user, Student student) {
        final long jwtId = user.getJwtId();
        student.setUserId(jwtId);
        boolean modifyStudent = studentService.modify(student);
        if (modifyStudent) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "修改成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "修改失败", null);
    }


    /**
     * 更新学生信息
     *
     * @param student
     * @return
     */
    @PostMapping("/update")
    public ResponseMessage update(Student student) {
        boolean modifyStudent = studentService.modify(student);
        if (modifyStudent) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "修改成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "修改失败", null);
    }

    /**
     * 查询填报了某竞赛的所有学生
     *
     * @param cpId
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("/getRegisterStudents")
    public ResponseMessage getRegisterStudents(@RequestParam(value = "cpId") long cpId,
                                               @RequestParam(value = "page", defaultValue = "1") int page,
                                               @RequestParam(value = "rows", defaultValue = "5") int rows) {
        DataGridResultInfo<StudentDTO> dataGridResultInfo = studentService.findRegisterStudent(cpId, page, rows);
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "查询成功", dataGridResultInfo);
    }

    @PostMapping("/getStudentInfoByUserId")
    public ResponseMessage getStudentInfoByUserId(JwtUser user) {
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "查询成功", studentService.findByUserId(user.getJwtId()));
    }

    @PostMapping("/getStudentInfo")
    public ResponseMessage getStudentInfo(@RequestParam("userId") long userId) {
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "查询成功", studentService.findByUserId(userId));
    }

}
